<template>
  <div>
    <div class="dialog-header">
      <el-button @click="funcFilter">查询</el-button>
    </div>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      highlight-hover-row
      row-id="id"
      height="360"
      :data="tableData"
      :loading="loading"
      remote-sort
      @sort-change="sortList"
    >
      <vxe-table-column type="checkbox" width="60" />
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="130"
          field="name"
          title="姓名"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.name" placeholder="请输入姓名" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="130"
          field="phone"
          title="电话"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.phone" placeholder="请输入电话" clearable />
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
    <div slot="footer" class="dialog-footer">
      <el-button @click="funcCancel">取 消</el-button>
      <el-button type="primary" @click="funcSubmit('select')">推 送</el-button>
      <el-button type="primary" @click="funcSubmit('all')">全部推送</el-button>
    </div>
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import VXEUtils from "vxe-utils";
Vue.use(VXETable, VXEUtils, { mounts: ["cookie"] });
import { getDriverTeamList } from "@/api/driver";
import { pushNoticeOrSendSMS } from "@/api/news";
export default {
  name: "NoteManagePushDriver",
  data() {
    return {
      loading: false,
      tableData: [],
      emptyListQueryString: "",
      listQuery: {
        driverTypes: "0, 1",
        name: "",
        phone: "",
        noticCode: "",
        noticeType: 1,
        sendType: 0,
        allflag: 0,
        partyCodes: "",
        pageNum: 1,
        pageSize: 10,
        sortField: "",
        sortType: "",
        total: 0
      }
    };
  },
  created() {
    this.emptyListQueryString = JSON.stringify(this.listQuery)
  },
  methods: {
    findList(code) {
      this.loading = true;
      if (code) {
        this.listQuery.noticCode = code;
      }
      getDriverTeamList(this.listQuery).then(response => {
        this.tableData = response.data.records;
        this.listQuery.total = response.data.total;
        this.loading = false;
      });
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1
      this.findList()
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property;
        this.listQuery.sortType = order;
      } else {
        this.listQuery.sortField = "";
      }
      this.findList();
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findList();
    },
    // 全部推送/部分推送
    funcSubmit(type) {
      if (type === "all") {
        this.listQuery.allflag = 1;
        this.listQuery.partyCodes = "";
      } else {
        const checkedRowArray = this.$refs.xTable.getSelectRecords();
        if (!checkedRowArray || checkedRowArray.length === 0) {
          this.$message({ message: "请选择推送的入驻商", type: "warning" });
          return false;
        }
        this.listQuery.allflag = 0;
        this.listQuery.partyCodes = checkedRowArray
          .map(item => item.partyCode)
          .join(",");
      }
      const rLoading = this.openLoading();
      pushNoticeOrSendSMS(this.listQuery).then(response => {
        rLoading.close();
        this.$message({ message: "推送成功", type: "success" });
        this.listQuery = JSON.parse(this.emptyListQueryString)
        this.$emit("fatherComplete");
      });
    },
    // 取消
    funcCancel() {
      this.listQuery = JSON.parse(this.emptyListQueryString)
      this.$emit("fatherComplete");
    }
  }
};
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis),
.vxe-table .vxe-footer--column:not(.col--ellipsis),
.vxe-table .vxe-header--column:not(.col--ellipsis) {
  padding: 6px 0;
}
.dialog-header {
  padding-bottom: 10px;
}
.dialog-footer {
  margin: 15px 0 15px 0;
  width: 300px;
  float: right;
}
</style>
